<!--#include file="common/header.html"-->
<body>
<div class="container-fluid larry-wrapper">
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12">
            <!--头部搜索-->
            <section class="panel panel-padding">
                <form class="layui-form" action="/education/admin/station">
                    <div class="layui-form">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input class="layui-input" name="_query.keyword" placeholder="关键字">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button lay-submit class="layui-btn" lay-filter="search" id="search">查找</button>
                        </div>
                    </div>
                </form>
            </section>

            <!--列表-->
            <section class="panel panel-padding">
                <div class="group-button">
                    <button class="layui-btn layui-btn-small modal-catch"
                            data-params='{"content":".add-subcat", "title":"添加","type":"1", "full":"true", "maxmin":"true"}'>
                        <i class="iconfont">&#xe649;</i> 添加
                    </button>
                    <button class="layui-btn layui-btn-small layui-btn-danger ajax-all" data-name="checkbox" data-params='{"url": "/education/admin/station/delete"}'>
                        <i class="iconfont">&#xe626;</i> 删除
                    </button>
                </div>
                <div id="list" class="layui-form"></div>

                <div class="text-right" id="page"></div>
            </section>
        </div>
    </div>
</div>
<div class="add-subcat">
    <form class="layui-form layui-form-pane" action="/education/admin/station/save" id="form1">
        <input type="hidden" name="a.id">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-block">
                <input type="text" name="a.station_name" required jq-verify="required"  class="layui-input ">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">编号</label>
            <div class="layui-input-block">
                <input type="text" name="a.station_no" required jq-verify="required"  class="layui-input ">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-block">
                <input type="text" name="a.station_tel" required jq-verify="required"  class="layui-input ">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">自提点地址</label>
            <div class="layui-input-inline">
                <select name="a.province_id" id="pId" lay-filter="p1" style="width: 120px;" required jq-verify="required">
                    <option value="">请选择省</option>
                </select>
            </div>
            <div class="layui-input-inline" id="cityId" style="width: 120px;">
                <select name="a.city_id" lay-filter="city" required jq-verify="required">
                    <option value="">请选择市</option>
                </select>
            </div>
            <div class="layui-input-inline"  id="districtId" style="width: 120px;">
                <select name="a.district_id" lay-filter="district" required jq-verify="required">
                    <option value="">请选择县/区</option>
                </select>
            </div>
            <!--<div class="layui-form-mid layui-word-aux">此处只是演示联动排版，并未做联动交互</div>-->
        </div>

        <div class="layui-form-item" id="desc">
            <label class="layui-form-label">详细地址</label>
            <div class="layui-input-block">
                <input type="text" name="a.station_detail" required jq-verify="required"  class="layui-input ">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <input type="radio" name="a.station_status" title="启用" value="1" checked />
                <input type="radio" name="a.station_status" title="禁用" value="2" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">自提运费</label>
            <div class="layui-input-block">
                <input type="number" name="a.station_fee" required jq-verify="required" class="layui-input" value="0">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width:200px">封面图片</label>
            <div class="layui-input-block">
                <input type="file" name="file" class="layui-upload-file">
                <input type="hidden" name="a.station_img">
            </div>
            <div class="layui-input-inline"><div class="imgbox" style="width: 800px;"></div></div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">营业时间说明</label>
            <div class="layui-input-block">
                <textarea name="a.station_desc" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" jq-submit jq-filter="submit">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

</body>
<script id="list-tpl" type="text/html"
        data-params='{"url":"/education/admin/station","dataName":"stationData","pageid":"#page", "method":"post"}'>
    <table id="example" class="layui-table lay-even" data-name="stationData" data-tplid="list-tpl">
        <thead>
        <tr>
            <th width="30"><input type="checkbox" id="checkall" data-name="checkbox" lay-filter="check"
                                  lay-skin="primary"></th>
            <th>名称</th>
            <th>编号</th>
            <th>联系电话</th>
            <th>详细地址</th>
            <th width="70">状态</th>
            <th>更新时间</th>
            <th width="120">操作</th>
        </tr>
        </thead>
        <tbody>
        {{# layui.each(d.list, function(index, item){ }}
        <tr>
            <td><input type="checkbox" name="checkbox" value="{{ item.id}}" lay-skin="primary"></td>
            <td>{{ item.station_name }}</td>
            <td>{{ item.station_no }}</td>
            <td>{{ item.station_tel }}</td>
            <td>{{ item.station_detail }}</td>
            <!--<td>
                <div class="layer-photos-demo">
                    {{# var imgs = item.station_images && item.station_images.split(',')  }}
                    {{# if(imgs) { }}
                    {{# for(var i=0;i< imgs.length; i++){ }}
                    <img layer-src="/education{{ imgs[i] }}"  src="/education{{ imgs[i] }}"
                         onclick="viewImg();"
                         style="height:60px;"  alt="图片">
                    {{# } } }}
                </div>
            </td>-->

            <td><input type="checkbox" name="station_status" lay-skin="switch" lay-text="启用|停用"
                       {{#if (item.station_status == 1){ }}checked="checked" {{# } }} lay-filter="ajax" value="1"
                       data-params='{"url":"/education/admin/station/setStatus/{{ item.id}}","data":"id={{ item.id}}"}'>
            </td>
            <td>{{ item.update_time||'' }}</td>
            <td>
                <button class="layui-btn layui-btn-mini" onclick="_edit('{{item.id}}','{{item.station_name}}');">编辑</button>
                <button class="layui-btn layui-btn-mini layui-btn-danger ajax"
                        data-list='{"key":"id={{ item.id}}","msg":true,"render":"true","action":"del"}'
                        data-params='{"url": "/education/admin/station/delete/{{item.id}}","data":"", "method":"post", "confirm":"true"}'>
                    删除
                </button></td>
            </td>
        </tr>
        {{# }); }}
        </tbody>

    </table>
</script>
<script src="../js/layui/layui.js"></script>
<script type="text/javascript" src="../js/lib/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="../js/lib/rest.js"></script>
<script type="text/javascript" src="../../jsfile/jquery/jquery.form.js"></script>
<!--#include file="common/version.html"-->
<script type="text/javascript" src="js/area_select.js"></script>
<script>
    layui.use('user_list');

    layui.use(['jquery','upload'], function(exports) {
        var box;
        var url_ext = '';
        //上传文件设置
        layui.upload({
            elem: '.layui-upload-file',
            url: '/education/api/image/station',
            before: function (input) {
                box = $(input).parent('form').parent('div').parent('.layui-input-block');
                if (box.next('div').length > 0) {
                    box.next('div').html('<div class="imgbox"><p>上传中...</p></div>');
                } else {
                    box.after('<div class="layui-input-block"><div class="imgbox"><p>上传中...</p></div></div>');
                }
            },
            success: function (res) {
                if (res.code == 200) {
                    box.next('div').find('div.imgbox').html('<img src="/education' + res.data[0].url + '" alt="..." class="img-thumbnail">');
                    box.find('input[name="a.station_img"]').val(res.data[0].url);
                } else {
                    box.next('div').find('p').html('上传失败...')
                }
            }
        });
    });

    function initData(){
        var $v = '{"url":"/education/admin/station?_query.productId='+ getQueryString('ids')
            +'","dataName":"stationData","pageid":"#page", "method":"post", "data":"orderColunm=id&orderMode=desc"}';
        $("#list-tpl").attr('data-params', $v);
    }

    function viewImg() {
        layui.use('layer', function(){
            var layer = layui.layer;
            layer.photos({
                photos: '.layer-photos-demo'
                ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
        });
    }

    function beforeAdd(obj){
        $('.imgbox').children().remove();
        $('#form1').attr('action', '/education/admin/station/save');
    }

    function _edit(ids, name) {
        var index = layer.open({
            type: 1,
            title:'编辑【'+ name +'】',
            content: $('.add-subcat')
        });
        layer.full(index);
        $('.imgbox').children().remove();
        $('#form1').attr('action', '/education/admin/station/update/' + ids);
        restGet('/education/admin/station/view/' + ids, null, $('#form1'), function(rs){
            if(rs.code == 200 || rs.status == 200){
                var str =  '<img src="/education{src}" alt="..." class="img-thumbnail" style="width: 200px;">';
                var imgDiv = $('input[name="a.station_img"]').parent().next().children('.imgbox');
                console.log( rs.data)
                imgDiv.append(str.replace(/{src}/g, rs.data['station_img']));
                $("select[name='a.province_id']").val(rs.data.province_id);
                var form = layui.form();
                // var select = 'dd[lay-value="' + rs.data.province_id + '"]';// 设置value
                // $('#pId').siblings("div.layui-form-select").find('dl').find(select).click();// 查找点击
                form.render();// 再次渲染select

                initSelectData(rs.data.province_id, rs.data.city_id);
                initSelectData2(rs.data.city_id, rs.data.district_id);
            }
        })
    }

    function initSelectData(val, cityId){
        var url = "/education/api/area?parentId=";
        $.post(url+val+'&level=2', function(rs){
            var str = "<option value=''></option>";
            $.each(rs.data, function(i,item){
                str += '<option value="' + item.id + '">'+ item.name +'</option>';
            });
            $("select[name='a.city_id']").html(str);
            $("select[name='a.city_id']").val(cityId);
            var form = layui.form();
            // $('#cityId').find('dl').find('dd[lay-value="' + cityId + '"]').click();// 查找点击
            form.render();
        });
    }
    function initSelectData2(val, districtId){
        var url = "/education/api/area?parentId=";
        $.post(url+val+'&level=3', function(rs){
            var str = "<option value=''></option>";
            $.each(rs.data, function(i,item){
                str += '<option value="' + item.id + '">'+ item.name +'</option>';
            });
            $("select[name='a.district_id']").html(str);
            var form = layui.form();
            form.render('select')
            $('#districtId').find('dd[lay-value="' + districtId + '"]').click();
            form.render();
        });
    }
</script>

</html>